package ip.youtube;

import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

import com.icesoft.faces.context.effects.JavascriptContext;

public class registredBean {
	
	private String name;	
	private String password;
	private String confirmPassword;
	private String registredfail;
	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getConfirmPassword() {
		return confirmPassword;
	}

	public void setConfirmPassword(String confirmPassword) {
		this.confirmPassword = confirmPassword;
	}

	public String getRegistredfail() {
		return registredfail;
	}

	public void setRegistredfail(String registredfail) {
		this.registredfail = registredfail;
	}

	public void registrade(ActionEvent e){
		if(password != confirmPassword){
			registredfail = "Passwords do not match";
		}
		else{
			UserDAO userdao = new UserDAO();		
		
			int user_id = userdao.registerNewUser(name, password);
			
			if(user_id == 0){
				registredfail = "User already exists";
			}
			else{
				FacesContext context = FacesContext.getCurrentInstance();
				// Valid registration
				//*html5 WebStorage*
				JavascriptContext.addJavascriptCall(context, "window.sessionStorage.setItem('authorized','yes')");
				JavascriptContext.addJavascriptCall(context, "window.sessionStorage.setItem('User_id', '" + user_id + "')");
			}
		}
	}

}
